import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import jwtDecode from 'jwt-decode'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    redirect: 'welcome',
    children: [{
      path: '/welcome',
      name: 'welcome',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ '../views/welcome.vue')
    }, {
      path: '/users',
      name: 'users',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ '../views/users.vue'),
      meta: {
        title: '用户管理',
        title1: '用户列表'
      }
    }, {
      path: '/roles',
      name: 'roles',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ '../views/roles.vue'),
      meta: {
        title: '权限管理',
        title1: '角色列表'
      }
    },
    {
      path: '/rights',
      name: 'rights',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ '../views/rights.vue'),
      meta: {
        title: '权限管理',
        title1: '权限列表'
      }
    },
    {
      path: '/goods',
      name: 'goods',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ '../views/goods.vue'),
      meta: {
        title: '商品管理',
        title1: '商品列表'
      }
    },{
      path: 'add',
      name: 'add',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ '../views/add.vue'),
      meta: {
        title: '商品管理',
        title1: '商品添加'
      }
    }, {
      path: '/params',
      name: 'params',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ '../views/params.vue'),
      meta: {
        title: '商品管理',
        title1: '分类参数'
      }
    }, {
      path: '/categories',
      name: 'categories',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ '../views/categories.vue'),
      meta: {
        title: '商品管理',
        title1: '商品分类'
      }
    }, {
      path: '/orders',
      name: 'orders',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ '../views/orders.vue'),
      meta: {
        title: '订单管理',
        title1: '订单列表'
      }
    }, {
      path: '/reports',
      name: 'reports',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ '../views/reports.vue'),
      meta: {
        title: '数据统计',
        title1: '统计报表'
      }
    }]
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/login.vue')
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next();
  }


  // console.log(jwtToken);
  if (to.path !== '/login') {
    let token = sessionStorage.getItem('token');
    if (token) {
      let jwtToken = jwtDecode(token);
      let nowTime = parseInt(new Date().getTime() / 1000)
      if (nowTime - jwtToken.exp > 0) {
        next('/login')
      } else {
        next()
      }
    } else {
      next('/login')
    }

  }
})
export default router
